import { LoginSchema, RegisterSchema } from '@/schemas';
import { responseOk } from '@/server/apiResponse';
import { AppError } from '@/server/appError';
import { ErrorCode } from '@/server/common/error-code.enum';
import { login, register } from '@/server/controllers/authController';
import { zValidator } from '@hono/zod-validator';
import { Hono } from 'hono';

export const authRoutes = new Hono()
    .post(
        '/register',
        zValidator('json', RegisterSchema, async (result, c) => {
            if (!result.success) throw new AppError(ErrorCode.BAD_REQUEST);
        }),
        async (c) => {
            const { user_email, user_name, user_password } = c.req.valid('json');
            const _ = await register({ user_email, user_name, user_password });
            const resp = responseOk({ message: 'register success' });
            return c.json(resp, 200);
        }
    )
    .post(
        '/login',
        zValidator('json', LoginSchema, async (result, c) => {
            if (!result.success) throw new AppError(ErrorCode.BAD_REQUEST);
        }),
        async (c) => {
            const { user_email, user_password } = c.req.valid('json');
            const _ = await login({ user_email, user_password });
            return c.json({ ok: 'ok' }, 200);
        }
    );
